﻿using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Diagnostics;
using System.IO;


namespace MySQL_Backup_and_Restore
{
    public partial class BackupAr : Form
    {

        string filename = "";
        string orgfilename;
        const string password = "56t3DjAvsWxs60rk3cj5omoiGWh2Lp";


        public BackupAr()
        {
            InitializeComponent();
            orgfilename = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + "\\MySQL\\MySQL Server 5.5\\bin\\backup.sql";

        }

        #region RESTORE

        private void button_Restore_Click(object sender, EventArgs e)
        {
            progressBar1.Visible = true;

            OpenFileDialog f2 = new OpenFileDialog();
            f2.Title = "فتح ملف الحفظ";
            f2.Filter = "Backup files (*.backup)|*.backup";
            if (DialogResult.OK != f2.ShowDialog())
            {
                progressBar1.Visible = false;
                return;
            }

            filename = f2.FileName;

            backgroundRestore.RunWorkerAsync();
        }

        private void backgroundRestore_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                button1.Enabled = false;
                button_Backup.Enabled = false;
                button_Restore.Enabled = false;

                ProcessStartInfo proc;
                proc = null;
                if (System.Environment.Is64BitOperatingSystem)
                {
                    proc = new ProcessStartInfo(Application.StartupPath + "\\Restore64.bat");
                   
                    
                }
                else
                {
                    proc = new ProcessStartInfo(Application.StartupPath + "\\Restore.bat");
                }


                File.Delete(orgfilename);

                EncDec.Decrypt(filename, orgfilename, password);

                proc.WindowStyle = ProcessWindowStyle.Hidden;


                OperatingSystem os = Environment.OSVersion;

                Version vs = os.Version;

                if (os.Platform == PlatformID.Win32NT)
                {
                    switch (vs.Major)
                    {
                        case 6:
                            proc.Verb = "runas";
                            proc.WindowStyle = ProcessWindowStyle.Hidden;


                            break;
                        default:
                            break;
                    }
                }

                Process proccess = Process.Start(proc);

                proccess.WaitForExit();

                File.Delete(orgfilename);
            }
            catch (Exception ee)
            {
                Program.exl.LogException(ee);
                
            }            
        }

        private void backgroundRestore_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Visible = false;
            MessageBox.Show("Done");
            button1_Click(null, null);
        }

        private void backgroundRestore_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Visible = true;
        }

        #endregion


        #region BACKUP

        private void button_Backup_Click(object sender, EventArgs e)
        {

            progressBar1.Visible = true;

            SaveFileDialog f2 = new SaveFileDialog();
            f2.Title = "فتح ملف الحفظ";
            f2.Filter = "Backup files (*.backup)|*.backup";
            if (DialogResult.OK != f2.ShowDialog())
            {
                progressBar1.Visible = false;
                return;
            }

            filename = f2.FileName;

            backgroundBackup.RunWorkerAsync();

        }

        private void backgroundBackup_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                button1.Enabled = false;
                button_Backup.Enabled = false;
                button_Restore.Enabled = false;
                ProcessStartInfo proc;
                proc = null;
                if (System.Environment.Is64BitOperatingSystem)
                {
                    proc = new ProcessStartInfo(Application.StartupPath + "\\Backup64.bat");
                    
                }
                else
                {
                    proc = new ProcessStartInfo(Application.StartupPath + "\\Backup.bat");
                }
                proc.WindowStyle = ProcessWindowStyle.Hidden;

                OperatingSystem os = Environment.OSVersion;

                Version vs = os.Version;

                if (os.Platform == PlatformID.Win32NT)
                {
                    switch (vs.Major)
                    {
                        case 6:
                            proc.Verb = "runas";
                            proc.WindowStyle = ProcessWindowStyle.Hidden;

                            break;
                        default:
                            break;
                    }
                }

                Process proccess = Process.Start(proc);

                proccess.WaitForExit();

                File.Copy(orgfilename, filename, true);

                File.Delete(orgfilename);

                EncDec.Encrypt(filename, filename + ".enc", password);
                File.Copy(filename + ".enc", filename, true);
                File.Delete(filename + ".enc");
            }
            catch (Exception ee)
            {
                Program.exl.LogException(ee);
            }

        }

        private void backgroundBackup_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Visible = true;
        }

        private void backgroundBackup_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Visible = false;
            MessageBox.Show("Done");
            button1_Click(null, null);
        }

        #endregion


        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
